#include "Cube.h"
#include "Floor.h"
#include "Camera.h"
#include "SkyBox.hpp"
#include <iostream>

using namespace Nitro;

int main()
{
    glfwInit();
    Renderer::Init({4, 6, 16});
    auto window = glfwCreateWindow(1440, 960, APP_NAME, 0, 0);
    glfwMakeContextCurrent(window);
    gladLoadGLLoader(GLADloadproc(glfwGetProcAddress));
    glfwSetWindowSizeCallback(
        window, [](GLFWwindow*, int w, int h) { glViewport(0, 0, w, h); });
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    SkyBox skybox;
    Cube cube(2.0f);
    Floor floor(40.0f);
    FpsCamera camera;
    cube.position = {0.f, 1.f, 0.f};

    double currentTime, prevTime = glfwGetTime();
    float dx, dy, delta;

    camera.position = glm::vec3(0.0f, 1.0f, -10.0f);

    glEnable(GL_DEPTH_TEST);
    while (!glfwWindowShouldClose(window))
    {
        currentTime = glfwGetTime();
        delta = currentTime - prevTime;

        if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
        {
            dy += delta;
        }
        if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
        {
            dy -= delta;
        }
        if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
        {
            dx -= delta;
        }
        if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
        {
            dx += delta;
        }
        camera.processInput(dx, dy);
        dx = dy = 0.0;

        if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
        {
            camera.position += delta * 10.0f * camera.direction;
        }
        if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
        {
            camera.position -= delta * 10.0f * camera.direction;
        }

        Renderer::Clear();
        Renderer::ClearColor();
        camera.render();
        cube.render();
        floor.render();
        skybox.render();

        prevTime = currentTime;
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}